﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:Controls="clr-namespace:MahApps.Metro.Controls">

    <Style TargetType="{x:Type Controls:ToggleSwitchButton}">
        <Setter Property="IsTabStop"
                Value="False" />
        <Setter Property="OnSwitchBrush"
                Value="{DynamicResource AccentColorBrush}" />
        <Setter Property="OffSwitchBrush"
                Value="{DynamicResource GrayBrush4}" />
        <Setter Property="ThumbIndicatorBrush"
                Value="{DynamicResource BlackBrush}" />
        <Setter Property="ThumbIndicatorDisabledBrush"
                Value="{DynamicResource GrayBrush4}" />
        <Setter Property="ThumbIndicatorWidth"
                Value="13" />
        <Setter Property="BorderThickness"
                Value="2" />
        <Setter Property="BorderBrush"
                Value="{DynamicResource GrayBrush4}" />
        <Setter Property="Width"
                Value="70" />
        <Setter Property="Padding"
                Value="2" />
        <Setter Property="Height"
                Value="35" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Controls:ToggleSwitchButton}">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"
                                                                       Storyboard.TargetName="PART_BackgroundOverlay">
                                            <DiscreteDoubleKeyFrame KeyTime="0"
                                                                    Value="0.5" />
                                        </DoubleAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"
                                                                       Storyboard.TargetName="PART_BackgroundOverlay">
                                            <DiscreteDoubleKeyFrame KeyTime="0"
                                                                    Value="0.2" />
                                        </DoubleAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"
                                                                       Storyboard.TargetName="PART_BackgroundOverlay">
                                            <DiscreteDoubleKeyFrame KeyTime="0"
                                                                    Value="0.4" />
                                        </DoubleAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Grid x:Name="PART_SwitchTrack"
                              Margin="5">
                            <Border BorderThickness="{TemplateBinding BorderThickness}"
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    Background="{TemplateBinding Background}">
                                <Grid Margin="{TemplateBinding Padding}"
                                      ClipToBounds="True">
                                    <Rectangle Fill="{TemplateBinding OnSwitchBrush}" />
                                    <Rectangle Fill="{TemplateBinding OffSwitchBrush}"
                                               RenderTransformOrigin="0.5,0.5">
                                        <Rectangle.RenderTransform>
                                            <TranslateTransform x:Name="PART_BackgroundTranslate" />
                                        </Rectangle.RenderTransform>
                                    </Rectangle>
                                    <Rectangle x:Name="PART_BackgroundOverlay"
                                               Fill="{DynamicResource WhiteBrush}"
                                               Opacity="0" />
                                </Grid>
                            </Border>
                            <Rectangle x:Name="PART_ThumbIndicator"
                                       Fill="{TemplateBinding ThumbIndicatorBrush}"
                                       Width="{TemplateBinding ThumbIndicatorWidth}"
                                       HorizontalAlignment="Left">
                                <Rectangle.RenderTransform>
                                    <TranslateTransform x:Name="PART_ThumbTranslate" />
                                </Rectangle.RenderTransform>
                            </Rectangle>
                        </Grid>
                        <Thumb x:Name="PART_DraggingThumb">
                            <Thumb.Template>
                                <ControlTemplate>
                                    <Rectangle Fill="Transparent" />
                                </ControlTemplate>
                            </Thumb.Template>
                        </Thumb>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled"
                                 Value="False">
                            <Setter TargetName="PART_ThumbIndicator"
                                    Property="Fill"
                                    Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ThumbIndicatorDisabledBrush}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type Controls:ToggleSwitch}">
        <Setter Property="IsTabStop"
                Value="True" />
        <Setter Property="ToggleSwitchButtonStyle"
                Value="{StaticResource {x:Type Controls:ToggleSwitchButton}}" />
        <Setter Property="OnSwitchBrush"
                Value="{DynamicResource AccentColorBrush}" />
        <Setter Property="OffSwitchBrush"
                Value="{DynamicResource GrayBrush4}" />
        <Setter Property="ThumbIndicatorBrush"
                Value="{DynamicResource BlackBrush}" />
        <Setter Property="ThumbIndicatorDisabledBrush"
                Value="{DynamicResource GrayBrush4}" />
        <Setter Property="ThumbIndicatorWidth"
                Value="13" />
        <Setter Property="HorizontalContentAlignment"
                Value="Left" />
        <Setter Property="VerticalContentAlignment"
                Value="Center" />
        <Setter Property="Foreground"
                Value="{DynamicResource BlackBrush}" />
        <Setter Property="FontFamily"
                Value="{DynamicResource ToggleSwitchFontFamily}" />
        <Setter Property="FontSize"
                Value="{DynamicResource ToggleSwitchFontSize}" />
        <Setter Property="SnapsToDevicePixels"
                Value="True" />
        <Setter Property="Validation.ErrorTemplate"
                Value="{DynamicResource ValidationErrorTemplate}" />
        <Setter Property="HeaderTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock Text="{Binding Mode=OneWay}"
                               TextWrapping="Wrap"
                               FontWeight="Normal"
                               HorizontalAlignment="Left"
                               VerticalAlignment="Top"
                               Margin="0,0,0,5"
                               Opacity="0.65"
                               FontFamily="{DynamicResource ToggleSwitchHeaderFontFamily}"
                               FontSize="{DynamicResource ToggleSwitchHeaderFontSize}" />
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Controls:ToggleSwitch}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Padding="{TemplateBinding Padding}">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="Header"
                                                         Storyboard.TargetProperty="(UIElement.Opacity)"
                                                         Duration="0"
                                                         To="0.3" />
                                        <DoubleAnimation Storyboard.TargetName="Content"
                                                         Storyboard.TargetProperty="(UIElement.Opacity)"
                                                         Duration="0"
                                                         To="0.3" />
                                        <DoubleAnimation Storyboard.TargetName="Switch"
                                                         Storyboard.TargetProperty="(UIElement.Opacity)"
                                                         Duration="0"
                                                         To="0.75" />
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Grid Background="Transparent">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"
                                                  x:Name="firstCol" />
                                <ColumnDefinition Width="Auto"
                                                  x:Name="secCol" />
                            </Grid.ColumnDefinitions>
                            <ContentControl x:Name="Header"
                                            Grid.ColumnSpan="2"
                                            Content="{TemplateBinding Header}"
                                            ContentTemplate="{TemplateBinding HeaderTemplate}"
                                            IsTabStop="False" />
                            <ContentControl x:Name="Content"
                                            Grid.Row="1"
                                            Content="{TemplateBinding Content}"
                                            ContentTemplate="{TemplateBinding ContentTemplate}"
                                            IsTabStop="False"
                                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                            FontFamily="{TemplateBinding FontFamily}"
                                            FontSize="{TemplateBinding FontSize}" />
                            <Controls:ToggleSwitchButton x:Name="Switch"
                                                         Grid.Column="1"
                                                         Grid.Row="1"
                                                         FocusVisualStyle="{x:Null}"
                                                         SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                                         Style="{TemplateBinding ToggleSwitchButtonStyle}"
                                                         Background="{TemplateBinding Background}"
                                                         IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                                         OnSwitchBrush="{TemplateBinding OnSwitchBrush}"
                                                         OffSwitchBrush="{TemplateBinding OffSwitchBrush}"
                                                         ThumbIndicatorBrush="{TemplateBinding ThumbIndicatorBrush}"
                                                         ThumbIndicatorDisabledBrush="{TemplateBinding ThumbIndicatorDisabledBrush}"
                                                         ThumbIndicatorWidth="{TemplateBinding ThumbIndicatorWidth}"
                                                         VerticalAlignment="Center" />
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsChecked"
                                           Value="True" />
                                <Condition Property="Content"
                                           Value="{x:Null}" />
                            </MultiTrigger.Conditions>
                            <Setter TargetName="Content"
                                    Property="Content">
                                <Setter.Value>
                                    <Binding RelativeSource="{RelativeSource TemplatedParent}"
                                             Path="OnLabel"
                                             Mode="TwoWay" />
                                </Setter.Value>
                            </Setter>
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsChecked"
                                           Value="False" />
                                <Condition Property="Content"
                                           Value="{x:Null}" />
                            </MultiTrigger.Conditions>
                            <Setter TargetName="Content"
                                    Property="Content">
                                <Setter.Value>
                                    <Binding RelativeSource="{RelativeSource TemplatedParent}"
                                             Path="OffLabel"
                                             Mode="TwoWay" />
                                </Setter.Value>
                            </Setter>
                        </MultiTrigger>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Header}"
                                     Value="{x:Null}">
                            <Setter TargetName="Header"
                                    Property="Visibility"
                                    Value="Collapsed" />
                        </DataTrigger>
                        <Trigger Property="ContentDirection"
                                 Value="RightToLeft">
                            <Setter Property="HorizontalContentAlignment"
                                    Value="Right" />
                            <Setter TargetName="firstCol"
                                    Property="Width"
                                    Value="Auto" />
                            <Setter TargetName="secCol"
                                    Property="Width"
                                    Value="*" />
                            <Setter TargetName="Content"
                                    Property="Grid.Column"
                                    Value="1" />
                            <Setter TargetName="Switch"
                                    Property="Grid.Column"
                                    Value="0" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
